package com.shengwei.truck.driver.view.ui.mine.userinfo;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;
import androidx.lifecycle.Observer;
import com.shengwei.truck.driver.R;
import com.shengwei.truck.driver.bean.Result;
import com.shengwei.truck.driver.bean.User;
import com.shengwei.truck.driver.databinding.ActivityModifyMobileBinding;
import com.shengwei.truck.driver.utils.CommonUtil;
import com.shengwei.truck.driver.utils.MyToast;
import com.shengwei.truck.driver.view.ui.BaseActivity;
import com.shengwei.truck.driver.vm.ModifyMobileViewModel;

import java.lang.ref.WeakReference;

/**
 * 修改手机号码
 */
public class ModifyMobileActivity extends BaseActivity<ModifyMobileViewModel, ActivityModifyMobileBinding> implements View.OnClickListener {
    String mMobile;
    String mNewMobile;
    private String mSmsCode;
    private String mIdentify;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getLastIntent();
        initView();
        initLiveDataObersve();
    }
    /**
     * 获取上一页传递过来的数据
     */
    private void getLastIntent() {
        Intent i = this.getIntent();
        Bundle b = i.getExtras();
        if(b != null) {
            mMobile = b.getString("mobile");
            mDataBinding.setMobile(mMobile);
        }
    }

    private void initView() {
        mDataBinding.btGetSmsCode.setOnClickListener(this);
        mDataBinding.btSureModify.setOnClickListener(this);
    }

    /**
     * 创建liveData观察者，用于监听数据变化，从而更新UI
     */
    private void initLiveDataObersve() {

        //监听短信接口返回的数据
        mViewModel.getSmsCodeData().observe(this, new Observer<Result<String>>() {
            @Override
            public void onChanged(Result<String> result) {
                result.handler(new OnCallback<String>() {
                    @Override
                    public void onSuccess(String data) {
                       Toast.makeText(ModifyMobileActivity.this,R.string.smscode_ok,Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });

        //监听 “修改手机号接口” 返回的数据
        mViewModel.getModifyMobileData().observe(this, new Observer<Result<String>>() {
            @Override
            public void onChanged(Result<String> result) {
                result.handler(new OnCallback<String>() {
                    @Override
                    public void onSuccess(String data) {
                        //直接跳转到登录页面
                        User user = User.getInstance(ModifyMobileActivity.this);
                        user.setToken("");
                        Intent i = new Intent(ModifyMobileActivity.this, LoginActivity.class);
                        i.putExtra("username", mNewMobile);
                        startActivity(i);
                        String modifyOk = ModifyMobileActivity.this.getResources().getString(R.string.modify_ok);
                        MyToast.showToastCenter(new WeakReference<>(ModifyMobileActivity.this),modifyOk,Toast.LENGTH_SHORT,true);
                    }
                });
            }
        });
    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_modify_mobile;
    }

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.btGetSmsCode) {
            getSmsCode();
        }
        else if(v.getId() == R.id.btSureModify) {
            mNewMobile = mDataBinding.edtMobileNum.getText().toString().trim();      //手机号
            mSmsCode = mDataBinding.edtSmsCode.getText().toString().trim();
            mIdentify = mDataBinding.edtIdentifyId.getText().toString().trim();
            boolean isInputCorrect = checkInput();
            if(isInputCorrect) {
                mViewModel.updateMobile(mIdentify,mNewMobile,mSmsCode);
            }
        }
    }

    /**
     * 检查用户输入的字段是否合法
     * @return boolean : true合法，false不合法
     */
    public boolean checkInput() {
        boolean isCorrect = false;
        WeakReference<Context> weakReferenceContext = new WeakReference<>(ModifyMobileActivity.this);

        //身份证号
        isCorrect = CommonUtil.checkIdentify(mIdentify,weakReferenceContext);
        //手机号
        if(isCorrect) {
            isCorrect = CommonUtil.checkMobile(mNewMobile,weakReferenceContext,true);
        }
        //短信码
        if(isCorrect) {
            if (TextUtils.isEmpty(mSmsCode)) {
                Toast.makeText(this,R.string.smscode_empty,Toast.LENGTH_SHORT).show();
                isCorrect = false;
            }
        }
        return isCorrect;
    }
    /**
     * 获取短信验证码
     */
    private void getSmsCode() {
        String mobile = mDataBinding.edtMobileNum.getText().toString();
        WeakReference<Context> weakReferenceContext = new WeakReference<>(ModifyMobileActivity.this);
        //手机号格式检查
        boolean isCorrect = CommonUtil.checkMobile(mobile,weakReferenceContext,true);
        if(isCorrect) {
            mViewModel.getSmsCode(mobile,"UPDATE_PHONE");
        }
    }
}